package com.yogurt.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yogurt.edu.entity.Teacher;
import com.yogurt.edu.entity.vo.TeacherQuery;
import com.yogurt.edu.service.TeacherService;
import com.yogurt.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author yogurt
 * @since 2020-07-02
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    //查询所有讲师
    @GetMapping("/findAll")
    public Result findAll(){
        List<Teacher> teachers = teacherService.list();
        return Result.ok().data("teachers",teachers);
    }

    //根据id逻辑删除
    @DeleteMapping("{id}")
    public Result removeById(@PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    //分页查询信息
    @GetMapping("/findByPage/{currentPage}/{row}")
    public Result findByPage(@PathVariable Long currentPage,@PathVariable Long row){
        //创建分页对象
        Page<Teacher> page = new Page<>(currentPage,row);
        teacherService.page(page);
        long total = page.getTotal(); //总记录数
        List<Teacher> records = page.getRecords(); //每页的数据List集合
        return Result.ok().data("total",total).data("rows",records);
    }

    //条件分页查询
    @PostMapping("/findByCondition/{currentPage}/{row}")
    public Result findByCondition(@PathVariable Long currentPage, @PathVariable Long row,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建分页对象
        Page<Teacher> page = new Page<>(currentPage,row);
        //构建查询条件
        QueryWrapper wrapper = new QueryWrapper();

        //获取所有的查询条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        teacherService.page(page, wrapper);

        long total = page.getTotal(); //总记录数
        List<Teacher> records = page.getRecords(); //每页的数据List集合
        return Result.ok().data("total",total).data("rows",records);
    }

    //新增讲师
    @PostMapping("/addTeacher")
    public Result addTeacher(@RequestBody Teacher teacher){
        boolean flag = teacherService.save(teacher);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    //根据id查询讲师列表
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        if (teacher != null){
            return Result.ok().data("teacher",teacher);
        }else {
            return Result.error();
        }
    }

    //修改讲师信息
    @PutMapping("/editTeacher")
    public Result editTeacher(@RequestBody Teacher teacher){
        boolean flag = teacherService.updateById(teacher);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
}

